package com.idanchuang.component.consumer.servlet;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author yjy
 * @date 2020/8/7 17:45
 **/
public class ConsumerServletRequestContext {


    public Map<String, String> getHeaders(Collection<String> headerNames) {
        Map<String, String> headers = new HashMap<>();
        if (headerNames == null || headerNames.isEmpty()) {
            return headers;
        }
        HttpServletRequest request = getRequest();
        if (request == null) {
            return headers;
        }
        for (String headerName : headerNames) {
            String value = request.getHeader(headerName);
            headers.put(headerName, value);
        }
        return headers;
    }

    public Map<String, List<String>> getMultiHeaders(Collection<String> headerNames) {
        Map<String, List<String>> headers = new HashMap<>();
        if (headerNames == null || headerNames.isEmpty()) {
            return headers;
        }
        HttpServletRequest request = getRequest();
        if (request == null) {
            return headers;
        }
        for (String headerName : headerNames) {
            Enumeration<String> values = request.getHeaders(headerName);
            if (values != null && values.hasMoreElements()) {
                List<String> vs = new ArrayList<>();
                while (values.hasMoreElements()) {
                    vs.add(values.nextElement());
                }
                headers.put(headerName, vs);
            }
        }
        return headers;
    }

    public Map<String, String> getQueries(Collection<String> queriesNames) {
        Map<String, String> queries = new HashMap<>();
        if (queriesNames == null || queriesNames.isEmpty()) {
            return queries;
        }
        HttpServletRequest request = getRequest();
        if (request == null) {
            return queries;
        }
        for (String queriesName : queriesNames) {
            queries.put(queriesName, request.getParameter(queriesName));
        }
        return queries;
    }

    public Map<String, String[]> getMultiQueries(Collection<String> queriesNames) {
        Map<String, String[]> queries = new HashMap<>();
        if (queriesNames == null || queriesNames.isEmpty()) {
            return queries;
        }
        HttpServletRequest request = getRequest();
        if (request == null) {
            return queries;
        }
        for (String queriesName : queriesNames) {
            queries.put(queriesName, request.getParameterValues(queriesName));
        }
        return queries;
    }

    private HttpServletRequest getRequest() {
        try {
            Class.forName("org.springframework.web.context.request.RequestContextHolder");
            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
            if (requestAttributes != null) {
                return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

}
